/**
 * @Description: TODO
 * @author: gongth
 * @date: 2022年07月20日 15:49
 */
package me.zhengjie.inter.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.business.service.ZhsqService;
import me.zhengjie.business.service.dto.PayInfoQueryCriteria;
import me.zhengjie.business.service.dto.ZhsqDto;
import me.zhengjie.mi.old.EasyGmUtils;
import me.zhengjie.utils.PageUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author gongtuohao
 * @date 2022-07-20
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "移动端支付")
@RequestMapping("/mp/pay")
public class MpPayController {

    private final ZhsqService zhsqService;

    @GetMapping(value = "/payInfo")
    @ApiOperation("获取支付信息")
    public ResponseEntity<Object> payInfo(@RequestParam Long bisZhsqId) {

        ZhsqDto zhsqDto = zhsqService.findById(bisZhsqId);
        Map<String, String> map = new HashMap<>();
        map.put("geRenBh",zhsqDto.getBisZhsqGrbh()); //医保个人编号，测试固定
        map.put("hospitalCode",zhsqDto.getBisZhsqHljgBm()); //医院编号，平台分配

        SimpleDateFormat formatter= new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date(System.currentTimeMillis());
        System.out.println(formatter.format(date));
        map.put("timeStamp",formatter.format(date));


        String clientid="6266306ACCCFEA63";
        String plainString = map.get("geRenBh");
        byte[] prvkey = Base64.getDecoder().decode("ZfGn/WK9i6LjblYmJnr+lLhkXXNr8my9v95VWQYhJwc=");
//        byte[] pubkey = Base64.getDecoder().decode("BB0muSDZU/kdNnt+PgaZCi6VEIFZfE6yEDh4ZHGgywi7Y2TCYHEj87PbW6MGLKFQpistB04gOd4na2elKpBYctg=");
        byte[] asig = EasyGmUtils.signSm3WithSm2(plainString.getBytes(StandardCharsets.UTF_8), clientid.getBytes(StandardCharsets.UTF_8), prvkey);
        String signstr=new String(Base64.getEncoder().encode(asig));

        map.put("sign",signstr);
        Map<String,Object> rsMap = PageUtil.toMpContent(map);
        return new ResponseEntity<>(rsMap, HttpStatus.OK);
    }
    public static void main(String[] args) throws IOException {


        String clientid="6266306ACCCFEA63";
        String plainString = "4050010731";
        byte[] prvkey = Base64.getDecoder().decode("ZfGn/WK9i6LjblYmJnr+lLhkXXNr8my9v95VWQYhJwc=");
        byte[] pubkey = Base64.getDecoder().decode("BB0muSDZU/kdNnt+PgaZCi6VEIFZfE6yEDh4ZHGgywi7Y2TCYHEj87PbW6MGLKFQpistB04gOd4na2elKpBYctg=");
        byte[] asig = EasyGmUtils.signSm3WithSm2(plainString.getBytes(StandardCharsets.UTF_8), clientid.getBytes(StandardCharsets.UTF_8), prvkey);
        String signstr=new String(Base64.getEncoder().encode(asig));
        System.out.println("签名串str---->"+signstr);
        boolean verified = EasyGmUtils.verifySm3WithSm2(plainString.getBytes(StandardCharsets.UTF_8), clientid.getBytes(StandardCharsets.UTF_8), Base64.getDecoder().decode(signstr), pubkey);
        System.out.println("签名校验---->"+verified);

    }
}

   